/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.uhk.ppro.traveler.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;


/**
 *
 * @author dusi
 */
@Entity
public class TUser implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String username;
    private String email;
    private String password;
    private String location;
    private int locked;

    // Relationships
    @OneToMany(mappedBy="user", cascade=CascadeType.ALL)
    private List<TTrip> trips = new ArrayList<TTrip>();
    
    @OneToOne(cascade= CascadeType.ALL)
    private TBudget budget;

    // Constructors
    public TUser() {

        this.username = "John Doe";
        this.password = "password";
        this.location = "USA";
        this.locked = 1;
        this.budget = new TBudget();
    }

    public TUser(String username, String password) {

        this.username = username;
        this.password = password;
        this.locked = 1;
        this.budget = new TBudget();
    }
    
    // Actions
    public void calcRemainingTrips() {
        
        Collections.sort(trips);
        
        float remaining = this.getBudget().getDefaultState() + this.getBudget().getTotal() - this.getBudget().getMinBudget();
        float delta = (this.getBudget().getIncome() - this.getBudget().getExpense()) / 30;
        
        for (TTrip t : trips) {
            
            if(remaining > t.getPrice()) {
                
                t.setRemaining(0);
                remaining -= t.getPrice();
            }
                    
            else {
                
                remaining -= t.getPrice();
                t.setRemaining(Math.round(Math.abs(remaining/delta)));
            }
        }
    }

    // Accessors
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

     public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public List<TTrip> getTrips() {
        return trips;
    }

    public void setTrips(List<TTrip> trips) {
        this.trips = trips;
    }

    public TBudget getBudget() {
        return budget;
    }

    public void setBudget(TBudget budget) {
        this.budget = budget;
    }
    
    public int getLocked() {
        return locked;
    }

    public void setLocked(int locked) {
        this.locked = locked;
    }

    // General
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TUser)) {
            return false;
        }
        TUser other = (TUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "cz.uhk.ppro.traveler.entity.TUser[id=" + id + "]";
    }
}
